package com.zw.pdm.module.est.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.est.model.CardModel;
import com.zw.pdm.module.est.service.CardVerifyService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 工艺卡校对
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/est/verify")
@SuppressWarnings("all")
public class CardVerifyController extends BaseController {

    @Autowired
    private CardVerifyService cardVerifyService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;


    @GetMapping
    @RequiresPermissions(value = {"est:verify:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "est/verify/page";
    }

    /** 列表 */
    @ResponseBody
    @PostMapping("/list/back")
    public JsonResult cardList(CraMakeSheet craMakeSheet) {
        startPage();
        List<CraMakeSheet> list = cardVerifyService.findWithCardVerify(craMakeSheet);
        return jsonResult(list);
    }

    /** 子列表 */
    @ResponseBody
    @PostMapping("/slaveList/back")
    public JsonResult slaveList(CardModel card) {
        startPage();
        List<CardModel> list = cardVerifyService.findSlaveWithCardVerify(card);
        return jsonResult(list);
    }

    @ResponseBody
    @PostMapping("/list")
    public JsonResult cardList(Card card) {
        startPage();
        List<Card> list = cardVerifyService.findWithNewCardVerify(card);
        return jsonResult(list);
    }

    /** 审核页面  */
    @GetMapping("/{id}")
    @RequiresPermissions({"est:verify:edit"})
    public String info(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("card", cardVerifyService.getCardById(id));
        model.addAttribute("details", cardVerifyService.findCardDetailByCardId(id));
        return "est/verify/info";
    }

    /** 校对同意 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/success")
    @RequiresPermissions({"est:verify:edit"})
    public JsonResult success(Integer id) {
        return jsonResult(cardVerifyService.success(id));
    }

    /** 校对不同意 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/reject")
    @RequiresPermissions({"est:verify:edit"})
    public JsonResult reject(Card card) {
        return jsonResult(cardVerifyService.reject(card));
    }

    /** 转定额 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/preQuota/back")
    @RequiresPermissions({"est:verify:quota"})
    public JsonResult preQuota(Integer id) {
        return jsonResult(cardVerifyService.preQuota(id));
    }

    /** 转定额 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/quota/back")
    @RequiresPermissions({"est:verify:quota"})
    public JsonResult quota(Integer id) {
        return jsonResult(cardVerifyService.quota(id));
    }

    @ResponseBody
    @RepeatCommit
    @PostMapping("/quota")
    @RequiresPermissions({"est:verify:quota"})
    public JsonResult newQuota(Integer id) {
        return jsonResult(cardVerifyService.newQuota(id));
    }

    /** 批量转定额 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/quotaAll")
    @RequiresPermissions({"est:verify:quota"})
    public JsonResult newQuota(Integer[] ids) {
        return jsonResult(cardVerifyService.newQuotaAll(ids));
    }

    /** 撤消转定额 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/cancelQuota/back")
    @RequiresPermissions({"est:verify:cancel"})
    public JsonResult cancelQuota(Integer id) {
        return jsonResult(cardVerifyService.cancelQuota(id));
    }

    /** 撤消转定额 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/cancelQuota")
    @RequiresPermissions({"est:verify:cancel"})
    public JsonResult newCancelQuota(Integer id) {
        return jsonResult(cardVerifyService.newCancelQuota(id));
    }

}
